﻿using System.Diagnostics;
using System.Linq;
using BlogsAPI.Sites;
using BlogsAPI.Sites.FoursquareSharp;
using RssToolkit.Rss;
using System.Collections.Generic;
using System.IO;
using System;
using BlogsAPI;
using System.Threading;
using BlogsAPI.BlogsYandex;
using VkontakteSharp.Endpoints.Wall;
using VkontakteSharp.Enums;
using VkontakteSharp.Model;
using Photo = VkontakteSharp.Model.Photo;
using User = VkontakteSharp.Model.User;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BlogAPIUnitTest
{
    /// <summary>
    /// Проверим взаимодействие с Vkontakte
    /// </summary>
    [TestClass]
    public class VkontakteTest
    {
        //[Test(Description="Загружаем посты")]
        [TestMethod]
        public void LoadPosts()
        {
            Unauthenticated client = new Unauthenticated(BlogsAPI.Sites.Vkontakte.APIKey);
            string[] ids = { "271829324_94", "-38310481_169339", "93388_20904", "2943_4276" };
            ResponseClass<WallRecord2<WallRecord>> posts = client.GetById(ids);
            Assert.IsTrue(posts.Data.Records.Count > 0);
        }

        //[Test(Description = "Загружаем посты")]
        [TestMethod]
        public void LoadPosts2()
        {
            var vk = new Vkontakte();
            List<BlogPost> odata =
                vk.GetManyPost(new[] { "https://vk.com/wall107452048_2094" });
                //, 
                  //  "http://vk.com/wall137415112_147615", "http://vk.com/wall-41670861_426845" });
            Assert.IsTrue(odata.Count > 0);
        }

        //-41150947_143496:-33761036_218534:27692083_4901:-3550233_10988:788510_1491:4256849_11577:-42284766_2556:133745084_4640:599264_396:165028499_111:218976966_409:12041660_4479:101864_2673:8439570_6609:-61330359_29468:6564109_3038:-61237438_2970:254106_4026:4621933_1310:162773733_1150:489231_3371:-51323217_3291:-41888661_1230:4847333_7623:185265754_1640:194940687_3949:-60132044_483:-30352061_413580:-34668089_7685:186120427_6742:-12290767_12188:-50522422_4099:2567950_4968:-33229281_210147:-37495068_53912
        //[Test(Description = "Загружаем посты")]
        [TestMethod]
        public void LoadPosts3()
        {
            string[] IDs =
                "-41150947_143496:-33761036_218534:27692083_4901:-3550233_10988:788510_1491:4256849_11577:-42284766_2556:133745084_4640:599264_396:165028499_111:218976966_409:12041660_4479:101864_2673:8439570_6609:-61330359_29468:6564109_3038:-61237438_2970:254106_4026:4621933_1310:162773733_1150:489231_3371:-51323217_3291:-41888661_1230:4847333_7623:185265754_1640:194940687_3949:-60132044_483:-30352061_413580:-34668089_7685:186120427_6742:-12290767_12188:-50522422_4099:2567950_4968:-33229281_210147:-37495068_53912"
                    .Split(':');
            var client = new Unauthenticated(Vkontakte.APIKey);
            ResponseClass<WallRecord2<WallRecord>> data = client.GetById(IDs);
            Assert.IsTrue(data.Data.Records.Count > 0);
        }

        /// NOTE: FAILED
        //[Test(Description = "Загружаем посты")]
        [TestMethod]
        public void LoadVideo()
        {
            var client = new Unauthenticated(BlogsAPI.Sites.Vkontakte.APIKey);
            var ids = new[] { "39221240_989" };
            ResponseClass<WallRecord2<WallRecord>> posts = client.GetById(ids);
            Assert.IsTrue(posts.Data.Users.Count > 0);
        }
        /// NOTE: FAILED
        //[Test(Description = "Загружаем посты")]
        [TestMethod]
        public void LoadPhoto()
        {
            var client = new VkontakteSharp.Endpoints.Photos.Authenticated(BlogsAPI.Sites.Vkontakte.APIKey,"");
            var ids = new[] { "33113733_290003116" };
            VkontakteSharp.Model.Response<List<Photo>> posts = client.GetById(ids);
            //дело в том, что без аутентификации не возможно получить фотку,поэтому тут ошибка
            Assert.IsNotNull(posts.Error);
            Assert.IsTrue(posts.Error.Code == 5);
            Debug.WriteLine(posts.Error.Message);
            //Assert.Greater(posts.Data.Count, 0);
        }

        //[Test(Description = "Поиск")]
        [TestMethod]
        public void SearchTest()
        {
            VkontakteSharp.Endpoints.Newsfeeds.Authenticated client =
                new VkontakteSharp.Endpoints.Newsfeeds.Authenticated(Vkontakte.APIKey, "2facdad62facdad62facdad6dc2f97202622fac2facdad6775a8987a25c1503712ed0b1");
            var posts = client.Search5("has:photo",100,0,null,null,0,true);
            Assert.IsTrue(posts.Data.Count > 0);
        }

        //[Test(Description = "Получение данных о пользвоателях")]
        [TestMethod]
        public void SearchUser()
        {
            VkontakteSharp.Endpoints.Users.Authenticated client =
                new VkontakteSharp.Endpoints.Users.Authenticated(BlogsAPI.Sites.Vkontakte.APIKey, "2facdad62facdad62facdad6dc2f97202622fac2facdad6775a8987a25c1503712ed0b1");
            VkontakteSharp.Model.Response<List<User>> posts = client.GetById(new[] { "71679295" }, 
                ProfileFields.counters | ProfileFields.photo_medium | ProfileFields.photo_big | ProfileFields.last_seen | ProfileFields.city);
            Assert.IsTrue(posts.Data.Count > 0);
        }

        //40137578
        //[Test(Description = "Получение данных о группе")]
        [TestMethod]
        public void SearchGroup()
        {
            VkontakteSharp.Endpoints.Groups.Authenticated client =
                new VkontakteSharp.Endpoints.Groups.Authenticated(BlogsAPI.Sites.Vkontakte.APIKey, "2facdad62facdad62facdad6dc2f97202622fac2facdad6775a8987a25c1503712ed0b1");
            VkontakteSharp.Model.Response<List<Group>> posts = client.GetById(new[] { "12382740" },
                GroupFields.counters | GroupFields.members_count | GroupFields.verified | GroupFields.contacts);
            Assert.IsTrue(posts.Data.Count > 0);
        }

        //[Test(Description = "Получение списка чекинов")]
        [TestMethod]
        public void GetChekins()
        {
            var client =
                new VkontakteSharp.Endpoints.Places.Authenticated(BlogsAPI.Sites.Vkontakte.APIKey,
                    "3cf9c03bbd15f48a51cb4fa14004f723eeb474201d2ab1ce440dd8d14881b69bc4fd43862cba4d0857fef");
            var chekins = client.GetCheckins(55.083428f, 38.805256f, 100);
            Assert.IsNotNull(chekins);
            Assert.IsNotNull(chekins.Data);
            Assert.IsTrue(chekins.Data.Count > 0);
        }

        //[Test(Description = "Получение списка мест")]
        [TestMethod]
        public void GetPlaces()
        {
            var client =
                new VkontakteSharp.Endpoints.Places.Authenticated(BlogsAPI.Sites.Vkontakte.APIKey,
                    "7c8492d142a8e5f592694f27edcd5d27893ec177d0982475487493448fc90b3dc426b71865ce5c0fdf4b8");
            var chekins = client.GetPlaces(55.083428f, 38.805256f, 100);
            Assert.IsNotNull(chekins);
            Assert.IsNotNull(chekins.Data);
            Assert.IsTrue(chekins.Data.Count > 0);
        }

        //[Test(Description = "Получение списка мест из 4sq.com")]
        [TestMethod]
        public void GetPlacesFoursquare()
        {
            var client =
                new BlogsAPI.Sites.FoursquareSharp.Endpoints.Unauthenticated(Foursquare.APIKey);
            var chekins = client.GetPlaces(55.083428f, 38.805256f, 300);
            Assert.IsNotNull(chekins);
            Assert.IsNotNull(chekins.Data);
            Assert.IsTrue(chekins.Data.Venues.Count > 0);
            var byNow = chekins.Data.Venues.OrderByDescending(i => i.HereNow.Count).ToArray();
        }

        //[Test(Description = "Попытка выполнения кода")]
        [TestMethod]
        public void Execute()
        {
            var client =
                new VkontakteSharp.Endpoints.Execute.Authenticated(BlogsAPI.Sites.Vkontakte.APIKey,
                    "7c8492d142a8e5f592694f27edcd5d27893ec177d0982475487493448fc90b3dc426b71865ce5c0fdf4b8");
            var result = client.Execute("return [API.users.isAppUser(), API.friends.get()];");
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data.Count > 0);
            Assert.IsTrue(result.Data.Any(i=>!String.IsNullOrEmpty(i)));
        }
    }
}
